package com.travelsky.login.api;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 统一返回结果封装
 * @param <T>
 */
@Data
public class ResultData<T> implements Serializable {
    private final static long serialVersionUID= 2454896816291241428L;
    /** 结果状态 ,具体状态码参见ResultData.java*/
    private Integer status;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 后端返回结果
     */
    private T data;

    /**
     * 响应时间戳
     */
    private long timestamp ;

    public ResultData(){
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(HttpStatus.OK.value());
        resultData.setMessage(HttpStatus.OK.getReasonPhrase());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> success() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(HttpStatus.OK.value());
        resultData.setMessage(HttpStatus.OK.getReasonPhrase());
        return resultData;
    }

    public static <T> ResultData<T> fail(Integer code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setMessage(message);
        return resultData;
    }


}
